#!/bin/bash
#description: Storage easy start-stop script.
#by oilive
#DOMAN_HOME=Storage路径
DOMAIN_HOME=/weblogic/storage/
#启停用户
WLS_OWNER=root
#日志输出目录
LOGS_HOME=/weblogic/cpslogs/
#日志备份目录
LOGS_BACKHOME=/weblogic/cpslogs/logbacks/
#备份后缀名
d=`date "+%Y%m%d%H%M%S"`
#启动
function startUp(){
	echo -e "! \033[31m${1}-Storage\033[0m is \033[33mstarting\033[0m, please wait a moment!!!" 
	chown -R ${WLS_OWNER}:${WLS_OWNER} ${DOMAIN_HOME}
	if [ ! -d "${LOGS_BACKHOME}" ];then
		mkdir ${LOGS_BACKHOME}
	fi
	if [ -f "${LOGS_HOME}${1}.out" ];then
		mv ${LOGS_HOME}${1}.out ${LOGS_BACKHOME}${1}-${d}.out
	fi
	su - ${WLS_OWNER} -c "cd ${DOMAIN_HOME}; nohup java -jar -D -XX:MaxPermSize=1024m -Ddefault.client.encoding=UTF-8 -Ddefault.override.encoding=UTF-8 -Dfile.encoding=UTF-8 -Duser.region=CH ${1}-asyncMqStorageService-0.0.1-SNAPSHOT.jar >> ${LOGS_HOME}${1}.out 2>&1 &"
	echo -e "! \033[31m${1}-Storage\033[0m \033[33mstart\033[0m \033[32msuccessfully\033[0m!!!" 
}
#停止
function stopDown(){
	num=`ps -ef |grep java |grep -v grep |grep ${1}-asyncMqStorageService |wc -l` 
	if [ ${num} -eq 0 ];then 
		echo -e "! \033[31m${1}-Storage\033[0m \033[33mnot start\033[0m, so \033[33mno\033[0m need \033[31mstop\033[0m!!!" 
	else 
		WLS_PID=`ps -ef|grep java|grep ${1}-asyncMqStorageService|awk '{print $2}'`
		kill -9 $WLS_PID
		echo -e "! \033[31m${1}-Storage\033[0m \033[33mstopped\033[0m \033[32msuccessfully\033[0m!!!" 
	fi
}
#执行启停方法
function exec(){
	num=`ps -ef |grep java |grep -v grep |grep ${1}-asyncMqStorageService |wc -l` 
	if [[ ${num} -eq 0 ]] && [[ ${2} = "startUp" ]];then
		${2} ${1}
	elif [[ ${num} -eq 1 ]] && [[ ${2} = "stopDown" ]];then
		${2} ${1}
	else
		zt="\033[32mis running\033[0m"
		if [ ${2} = "stopDown" ];then
			zt="\033[33mnot start\033[0m"
		fi
		echo -e "! \033[31m${1}-Storage\033[0m ${zt}, so \033[33mno\033[0m need \033[33mexecute\033[0m \033[34m${2}\033[0m command again!!!"
	fi
}
APPS=(ent person)
function status(){
	for((i=0;i<${#APPS[@]};i++)) do
		echo "!---------------------------------------------!"
		app=${APPS[i]}
		num=`ps -ef |grep java |grep -v grep |grep ${app}-asyncMqStorageService |wc -l`
		if [ ${num} -eq 1 ];then
			echo -e "! \033[31m${app}-Storage\033[0m is \033[32mrunning\033[0m, \033[34mSee next line for process content\033[0m!!!"
			ps -ef |grep java |grep -v grep |grep ${app}-asyncMqStorageService
		else
			echo -e "! \033[31m${app}-Storage\033[0m \033[33mnot start\033[0m, so \033[33mdoesn't\033[0m have \033[31m${app}-Storage\033[0m \033[34mprocess\033[0m!!!" 
		fi
	done
	echo "!---------------------------------------------!"
}
#输出日志
function out(){
	echo -e "! \033[31mSee next line for ${1}_process content\033[0m!!!"
	tail -100f ${LOGS_HOME}${1}.out
}
case "$1" in
'restart')
	exec ent stopDown
	exec person stopDown
	exec ent startUp
	exec person startUp
;;
'start')
	exec ent startUp
	exec person startUp
;;
'estart')
	#startUp ent
	exec ent startUp
;;
'pstart')
	exec person startUp
;;
'stop')
	exec ent stopDown
	exec person stopDown
;;
'pstop')
	exec person stopDown
;;
'estop')
	exec ent stopDown
;;
'ps')
	status
;;
'ptail')
	out person
;;
'etail')
	out ent
;;
esac
